<template>
  <div class="wrap">
    <div class="handle-wrap">
      <h4>菜单权限</h4>
      <div>
        <el-button type="primary" @click="add">新增</el-button>
      </div>
    </div>

    <div class="main">
      <el-table :data="permissionList" style="width: 100%">
        <el-table-column prop="name" label="英文标识"> </el-table-column>
        <el-table-column prop="path" label="路由"> </el-table-column>
        <el-table-column prop="title" label="名称"> </el-table-column>
        <el-table-column prop="component" label="地址"> </el-table-column>
        <el-table-column prop="sort" label="排序"> </el-table-column> 
        <el-table-column fixed="right" label="操作" width="250">
        <template v-slot="scope">
          <el-button
            @click.stop="editorRow(scope.row)"
            type="primary"
            size="small"
          >
            编辑
          </el-button>
          <el-button
            @click.stop="deleteRow(scope.row)"
            size="small"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
      </el-table>
    </div>

    <el-dialog
      v-model="dialogFormVisible"
      :close-on-click-modal="false"
      :title="dialogTitle"
    >
      <el-form :model="form" label-position="top">
        <el-form-item label="路由" label-width="140px">
          <el-input v-model.trim="form.path" autocomplete="off" />
        </el-form-item>
        <el-form-item label="英文标识" label-width="140px">
          <el-input v-model.trim="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="地址" label-width="140px">
          <el-input v-model.trim="form.component" autocomplete="off" />
        </el-form-item>
        <el-form-item label="名称" label-width="140px">
          <el-input v-model.trim="form.title" autocomplete="off" />
        </el-form-item>
        <el-form-item label="排序" label-width="140px">
          <el-input v-model.trim="form.sort" autocomplete="off" />
        </el-form-item>
        
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="confirm"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import request from "utils/request";
import { ElMessage, ElMessageBox } from "element-plus";
export default {
  name: "JddChromePluginsAuthority",

  data() {
    return {
      dialogTitle: "新增",
      dialogFormVisible: false,
      form: {
        id: "",
        name: "",
        path: "",
        component: "",
        title: "",
        sort:0
      },
      permissionList: [
        {
          id: "",
          name: "",
          path: "",
          component: "",
          title: "",
          sort:0
        },
      ],
    };
  },

  mounted() {
    this.getPermissionList()
  },
  watch: {
    dialogFormVisible: {
      handler(value) {
        if (!value) {
          Object.assign(this.form, {
            id: "",
            name: "",
            path: "",
            component: "",
            title: "",
            sort:0
          });
          console.log("this.form", this.form);
        }
      },
    },
  },
  methods: {
    handleNodeClick(data) {
      console.log(data);
    },
    async getPermissionList() {
      let ret = await request.get("/getPermissionList", {});
      if (ret.code == 200) {
        this.permissionList = ret.data || [];
      }
    },
    add() {
      this.dialogTitle = "新增";
      this.dialogFormVisible = true;
    },
    editorRow(role) {
      this.dialogTitle = "编辑";
      this.dialogFormVisible = true;
      Object.assign(this.form, role);
    },
    async confirm() {
      let ret = await request.post("/modifyPermission", {
        id: this.form.id,
        name: this.form.name,
        path: this.form.path,
        component: this.form.component,
        title: this.form.title,
        sort: this.form.sort,
      });

      if (ret.code == 200) {
        this.$message({
          message: "编辑成功",
          type: "success",
          showClose: true,
          duration: 1000,
        });
        this.getPermissionList();
        this.dialogFormVisible = false;
      } else {
        this.$message({
          message: ret.message,
          type: "error",
          showClose: true,
          duration: 1000,
        });
      }
    },
    async deleteRow(row) {
      ElMessageBox.confirm("确认需要删除该菜单?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let ret = await request.post("/deletePermission", {
            id: row.id,
          });
          if (ret.code == 200) {
            this.$message({
              message: "移除成功",
              type: "success",
            });
            this.getPermissionList();
          } else {
            this.$message({
              message: ret.message,
              type: "error",
            });
          }
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "取消删除",
          });
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.wrap {
  .handle-wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .main {
  }
}
</style>
